<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>采购订单审批</title>
    <script type="text/javascript">
      var purchaseOrderAuditWin;
      var purchaseAuditForCancelWin;
      var purchaseOrderId;
      Ext.onReady(function(){

        var vendorTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var vendorStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/vendor/getAllVendors?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });

        var orderNumber = new Ext.form.TextField({
          fieldLabel: '单号',
          name: 'order_number',
          width:130
        });

        var vendor =  new Ext.form.ComboBox({
          fieldLabel: '供应商',
          store: vendorStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'vendor_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          width:140,
          //pageSize: 10,
          //hideTrigger: false,
          tpl: vendorTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          selectOnFocus:true
        });

        var stateStore = new Ext.data.ArrayStore({
          fields: ['text','value'],
          data : auditStatusArray
        });
        
        var auditingStatus = new Ext.form.ComboBox({
          fieldLabel: '审批状态',
          store: stateStore,
          displayField:'text',
          typeAhead: true,
          width:130,
          mode: 'local',
          name: 'auditing_status',
          forceSelection: true,
          triggerAction: 'all',
          editable:false,
          valueField: 'value',
          emptyText:'',
          forceSelection: true,
          selectOnFocus:true
        })

        var beginDate = new Ext.form.DateField({
          fieldLabel: '发货日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          }
        });
        var endDate = new Ext.form.DateField({
          fieldLabel: '至    ',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          width: '100',
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          inputType: '2010-01-01',
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date',
          listeners: {
            select: function(field, e){
              //Ext.MessageBox.alert('提示', '提交成功');
            }
          }
        });

        var submitButton = new Ext.Button({
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            purchaseOrderAuditDS.removeAll();
            purchaseOrderAuditDS.baseParams.order_number = orderNumber.getValue();
            purchaseOrderAuditDS.baseParams.auditing_status = auditingStatus.getValue();

            purchaseOrderAuditDS.baseParams.begin_date = beginDate.getRawValue();
            purchaseOrderAuditDS.baseParams.end_date = endDate.getRawValue();
            purchaseOrderAuditDS.baseParams.vendor_id = vendor.getValue();
            purchaseOrderAuditDS.load();
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 65,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 700},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [orderNumber]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [auditingStatus]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [vendor]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [beginDate]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [endDate]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //被选择的订单号。
        var purchaseOrderId;

        var purchaseOrderAuditDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/purchase_order/getAllPurchaseOrdersByAuditStatus'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'order_number'
            }, {
              name: 'action_date'
            }, {
              name: 'amount'
            }, {
              name: 'tax_amount',
              type: 'float'
            }, {
              name: 'value_added_tax',
              type: 'float'
            }, {
              name: 'description'
            }, {
              name: 'auditing_status'
            }, {
              name: 'audit_opinion'
            },{
              name: 'auditor_name',
              mapping:'auditor',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }, {
              name: 'vendor_name',
              mapping:'vendor.name'
            }, {
              name: 'department_name',
              mapping:'department.name'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            }, {
              name: 'deal_with_status'
            }, {
              name: 'store_status'
            }, {
              name: 'delivery_employee_name',
              mapping: 'delivery_employee',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        
        //生成采购订单Grid的CM
        var purchaseOrderAuditSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var purchaseOrderAuditCM = new Ext.grid.ColumnModel({
          defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),purchaseOrderAuditSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '采购单号',
              dataIndex: 'order_number',
              width: 130
            },{
              header: '供应商',
              dataIndex: 'vendor_name',
              width: 130
            },{
              header: '总金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '进项税',
              dataIndex: 'value_added_tax',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '总金额(含税)',
              dataIndex: 'tax_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 100
            },{
              header: '到货日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 60,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 50
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 150
            },{
              header: '处理状态',
              dataIndex: 'deal_with_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '入库状态',
              dataIndex: 'store_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '仓库操作员',
              dataIndex: 'delivery_employee_name',
              width: 70
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            }
          ]});

        //生成要订单的产品的Grid
        var purchaseOrderAuditGrid = new Ext.grid.GridPanel({
          title:'采购订单审批列表',
          store: purchaseOrderAuditDS,
          cm: purchaseOrderAuditCM,
          sm: purchaseOrderAuditSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-600,
          frame: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: purchaseOrderAuditDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });

        purchaseOrderAuditGrid.on('rowcontextmenu', function(grid, rowIndex, e){
          purchaseOrderId = purchaseOrderAuditDS.getAt(rowIndex).get('id');
          var auditingStatus = purchaseOrderAuditDS.getAt(rowIndex).get('auditing_status');
          auditOrder.setDisabled(true);
          auditOrderForCancel.setDisabled(true);
          if (auditingStatus === "申请新增" || auditingStatus === "申请变更"){
            auditOrder.setDisabled(false);
          }
          if (auditingStatus === "申请撤消"){
            auditOrderForCancel.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var auditOrder = new Ext.menu.Item({
          text: '【审批采购订单】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });

        var auditOrderForCancel = new Ext.menu.Item({
          text: '【审批采购订单  <font color=red>(撤消)</font>】',
          //iconCls: 'album',
          handler: rightMenuAuditForCancelFn
        });

        function rightMenuAuditFn(){
          purchaseOrderAuditWin = new Ext.Window({
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '采购订单审批',
            autoLoad: {
              url: "/purchase_order/auditPurchaseOrder?purchase_order_id="+purchaseOrderId,
              scripts: true,
              callback: function(options, success, response, request)
              { 
                if (response.responseText === "NoPrivilege"){
                  purchaseOrderAuditWin.close();
                }
              }
            }
          });
          purchaseOrderAuditWin.on("destroy",function(){
            purchaseOrderAuditDS.reload();
          });
          purchaseOrderAuditWin.resizable = false;
          purchaseOrderAuditWin.show();
        }

        function rightMenuAuditForCancelFn(){
          purchaseAuditForCancelWin = new Ext.Window({
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '采购订单审批<font color=red>(撤消)</font>',
            autoLoad: {
              url: "/purchase_order/auditPurchaseOrderForCancel?purchase_order_id="+purchaseOrderId,
              scripts: true
            }
          });
          purchaseAuditForCancelWin.on("destroy",function(){
            purchaseOrderAuditDS.reload();
          });
          purchaseAuditForCancelWin.show();
        }
 
        var rightClick = new Ext.menu.Menu({
          items: [auditOrder,auditOrderForCancel]
        });
        
        purchaseOrderAuditDS.load();

        purchaseOrderAuditGrid.on('rowdblclick', function(gridPanel, rowIndex, e){
          purchaseOrderId = purchaseOrderAuditDS.getAt(rowIndex).get('id');
          var purchaseOrderNumber = purchaseOrderAuditDS.getAt(rowIndex).get('order_number');
          //purchaseOrderAuditDS.reload();
          purchaseOrderDetailDS.removeAll();
          purchaseOrderDetailDS.baseParams.purchase_order_id = purchaseOrderId;
          purchaseOrderDetailDS.load();
          purchaseOrderDetailGrid.setTitle("采购订单："+purchaseOrderNumber+"，的详细信息");
        });

        //--------------------------采购订单详细，双击后可显示数据----------------
        //采购订单Grid Store Detail
        var purchaseOrderDetailDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/purchase_order/getPurchaseOrderDetailsById'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'sale_order_number',
              mapping: 'sale_order',
              convert:function(value,record){
                if(value == null){
                  return "";
                }else{
                  return value.order_number;
                }
              }
            },{
              name: 'sale_order_status',
              mapping: 'sale_order',
              convert:function(value,record){
                if(value == null){
                  return "";
                }else{
                  return value.deal_with_status;
                }
              }
            },{
              name: 'product_name',
              mapping:'product.name'
            },{
              name: 'product_number',
              mapping:'product.number'
            },{
              name: 'product_spec',
              mapping:'product.spec'
            },{
              name: 'quantity'
            }, {
              name: 'unit_price'
            }, {
              name: 'amount'
            }, {
              name: 'tax_price'
            }, {
              name: 'tax_amount'
            }, {
              name: 'value_added_tax',
              type: 'float'
            }, {
              name: 'description'
            },{
              name: 'is_suit',
              mapping:'product.is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }])
        });

        //生成采购订单详细表的Grid的CM
        var purchaseOrderDetailSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var purchaseOrderDetailCM = new Ext.grid.ColumnModel({
          defaults: {
            sortable: true,
            menuDisabled: false
          },columns: [new Ext.grid.RowNumberer(),purchaseOrderDetailSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '销售订单单号',
              dataIndex: 'sale_order_number',
              width: 210
            },{
              header: '销售订单状态',
              dataIndex: 'sale_order_status',
              width: 90
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 230
            },{
              header: '编号',
              dataIndex: 'product_number',
              width: 70
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 70
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 60
            },{
              header: '单价',
              dataIndex: 'unit_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.0000000000'),
              width: 120
            },{
              header: '总价',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 80
            },{
              header: '进项税',
              dataIndex: 'value_added_tax',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '单价(含税)',
              dataIndex: 'tax_price',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 80
            },{
              header: '总价(含税)',
              dataIndex: 'tax_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 85
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }
          ]});
        //生成要订单的产品的Grid
        var purchaseOrderDetailGrid = new Ext.grid.GridPanel({
          title:'订单详细列表',
          store: purchaseOrderDetailDS,
          cm: purchaseOrderDetailCM,
          sm: purchaseOrderDetailSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-620,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });


        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: screenHeight-200,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          /*layout: {
            type:'vbox',
            align:'stretch'
          },*/
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[queryFormPanel,purchaseOrderAuditGrid,purchaseOrderDetailGrid]
        });
        workbenchPanel.render("purchase_order_audit_list");

      })
    </script>
  </head>
  <body>
    <div id="purchase_order_audit_list"></div>
  </body>
</html>
